<template>
	<view class="login-box"> 
	<view class="logo">
			<img class="logo-img" src="https://s2.loli.net/2022/06/26/7RdbigFvaotrSeZ.png" />
			<!-- <img class="logo-img" style="width: 100%;height: 100%" :src="logoImg" /> -->
		 </view>
		<view class="t-login"
		<!-- 标题 -->
		<form class="cl">
		 
			<view class="t-b">{{ title }}</view>
			<view class="t-a">
				<u-input 
				    prefixIcon="account"
	                prefixIconStyle="font-size: 22px;color: #909399"
				    placeholder="请输入账号"
				    border="surround"
					style="height: 35px"
					v-model="username"
				    clearable
				  ></u-input>
			</view>
			<view class="t-a">
				<u-input
				    prefixIcon="eye"
					prefixIconStyle="font-size: 22px;color: #909399"
				    placeholder="请输入密码"
					type="password"
				    border="surround"
					style="height: 35px"
					v-model="password"
				    clearable
				  ></u-input>
			</view>
			<!-- <view class="t-a">
				<image src="@/static/sj.png"></image>
				<input type="number" name="phone" placeholder="请输入手机号" maxlength="11" v-model="phone" />
			</view>
			<view class="t-a">
				<image src="@/static/yz.png"></image>
				<input type="number" name="code" maxlength="6" placeholder="请输入验证码" v-model="yzm" />
				<view v-if="showText" class="t-c" @click="getCode()">发送短信</view>
				<view v-else class="t-c" style="background-color: #A7A7A7;">重新发送({{ second }})</view>
			</view> -->
			<u-button class="custom-style" color="#697AE4" @click="login" text="登 录"></u-button>
			<!-- <button @click="login" style="margin-top: 50rpx;">登 录</button> -->
		</form>
	</view>
	</view>
	
</template>
<script>
import { login } from '@/api/common.js'
export default {
	data() {
		return {
			title: '欢迎登录小窝记账',
			second: 60, //默认60秒
			showText: true, //判断短信是否发送
			phone: '', //手机号码
			yzm: '', //验证码
			username: '',
			password: '',
			logoImg: require('@/static/login-logo.png')
		};
	},
	onLoad() {
		if(uni.getStorageSync('token')) {
			uni.navigateTo({
				url: '/pages/index/index'
			});
		}
	},
	methods: {
		//当前登录按钮操作
		async login() {
			// if (!this.phone) {
			// 	uni.showToast({ title: '请输入手机号', icon: 'none' });
			// 	return;
			// }
			// if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone)) {
			// 	uni.showToast({ title: '请输入正确手机号', icon: 'none' });
			// 	return;
			// }
			// if (!this.yzm) {
			// 	uni.showToast({ title: '请输入验证码', icon: 'none' });
			// 	return;
			// }
			if (!this.username) {
				uni.showToast({ title: '请输入账号', icon: 'none' });
				return;
			}
			if (!this.password) {
				uni.showToast({ title: '请输入密码', icon: 'none' });
				return;
			}
			const data = {
				username: this.username,
				password: this.password
			}
			const rsp = await login(data, { custom: { auth: false }})
			if(rsp.code === 0) { 
				uni.setStorageSync('token', rsp.data.accessToken);
				this.$store.commit('common/changeUserInfo', rsp.data.user)
				this.$store.commit('common/changeToken', { access_token: rsp.data.accessToken, refresh_token: rsp.data.refreshToken })
				uni.showToast({ title: '登录成功', icon: 'success' });
				uni.navigateTo({
					url: '/pages/index/index'
				});
			}
			
		},
		//获取短信验证码
		getCode() {
			var interval = setInterval(() => {
				this.showText = false;
				var times = this.second - 1;
				this.second = times;
				console.log(times);
			}, 1000);
			setTimeout(() => {
				clearInterval(interval);
				this.second = 60;
				this.showText = true;
			}, 60000);
			//这里请求后台获取短信验证码
			uni.request({
				//......//此处省略
				success: function(res) {
					this.showText = false;
				}
			});
		},
	}
};
</script>
<style lang="scss" scoped>	

.t-login {
	width: 600rpx;
	font-size: 28rpx;
	color: #000;
}

.custom-style {
	border-radius: 8px;
	margin-top: 60rpx;
}

.t-login button {
	font-size: 28rpx;
	background: #697AE4;
	color: #fff;
	height: 90rpx;
	line-height: 90rpx;
	border-radius: 50rpx;
	box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
}

.t-login input {
	height: 90rpx;
	padding: 0 20rpx 0 120rpx;
	line-height: 90rpx;
	margin-bottom: 50rpx;
	background: #f8f7fc;
	border: 1px solid #e9e9e9;
	font-size: 28rpx;
	border-radius: 50rpx;
}

 .t-a {
	margin-bottom: 35rpx;
}

.logo {
	display: grid;   
	align-items: center;
	justify-content: center;
	margin-top: 160rpx;
	.logo-img {
		width: 460rpx;
	    height: 360rpx;
	}
}
.cl .t-b {
	font-size: 46rpx;
	color: #000;
	padding-bottom: 80rpx;
	font-weight: bold;
}

.t-login .t-c {
	position: absolute;
	right: 22rpx;
	top: 22rpx;
	background: #697AE4;
	color: #fff;
	font-size: 24rpx;
	border-radius: 50rpx;
	height: 50rpx;
	line-height: 50rpx;
	padding: 0 25rpx;
}








.cl {
	position: absolute;
	width: 100%;
	top: 50%;
	left: 50%;
	width: 80%;
	transform: translate(-50%, -50%);
}

</style>
